﻿<script lang="ts" name="appVehicleInfo" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormRules } from "element-plus";
import { formatDate } from '/@/utils/formatTime';
import { useAppVehicleInfoApi } from '/@/api/vehicle-info/appVehicleInfo';

//父级传递来的函数，用于回调
const emit = defineEmits(["reloadTable"]);
const appVehicleInfoApi = useAppVehicleInfoApi();
const ruleFormRef = ref();

const state = reactive({
	title: '',
	loading: false,
	showDialog: false,
	ruleForm: {} as any,
	stores: {},
	dropdownData: {} as any,
});

// 自行添加其他规则
const rules = ref<FormRules>({
  sellerId: [{required: true, message: '请选择卖家Id！', trigger: 'blur',},],
  title: [{required: true, message: '请选择标题！', trigger: 'blur',},],
  brandId: [{required: true, message: '请选择品牌Id！', trigger: 'blur',},],
  seriesId: [{required: true, message: '请选择车系Id！', trigger: 'blur',},],
  modelId: [{required: true, message: '请选择车型Id！', trigger: 'blur',},],
  price: [{required: true, message: '请选择价格！', trigger: 'blur',},],
  mileage: [{required: true, message: '请选择里程数！', trigger: 'blur',},],
  vehicleType: [{required: true, message: '请选择车辆类型！', trigger: 'blur',},],
  transmission: [{required: true, message: '请选择变速箱类型！', trigger: 'blur',},],
  fuelType: [{required: true, message: '请选择燃油类型！', trigger: 'blur',},],
  conditionLevel: [{required: true, message: '请选择车况等级！', trigger: 'blur',},],
  status: [{required: true, message: '请选择状态！', trigger: 'blur',},],
});

// 页面加载时
onMounted(async () => {
});

// 打开弹窗
const openDialog = async (row: any, title: string) => {
	state.title = title;
	row = row ?? {  };
	state.ruleForm = row.id ? await appVehicleInfoApi.detail(row.id).then(res => res.data.result) : JSON.parse(JSON.stringify(row));
	state.showDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit("reloadTable");
	state.showDialog = false;
};

// 提交
const submit = async () => {
	ruleFormRef.value.validate(async (isValid: boolean, fields?: any) => {
		if (isValid) {
			let values = state.ruleForm;
			await appVehicleInfoApi[state.ruleForm.id ? 'update' : 'add'](values);
			closeDialog();
		} else {
			ElMessage({
				message: `表单有${Object.keys(fields).length}处验证失败，请修改后再提交`,
				type: "error",
			});
		}
	});
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
<template>
	<div class="appVehicleInfo-container">
		<el-dialog v-model="state.showDialog" :width="800" draggable :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ state.title }}</span>
				</div>
			</template>
			<el-form :model="state.ruleForm" ref="ruleFormRef" label-width="auto" :rules="rules">
				<el-row :gutter="35">
					<el-form-item v-show="false">
						<el-input v-model="state.ruleForm.id" />
					</el-form-item>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="卖家Id" prop="sellerId">
							<el-input v-model="state.ruleForm.sellerId" placeholder="请输入卖家Id" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="标题" prop="title">
							<el-input v-model="state.ruleForm.title" placeholder="请输入标题" maxlength="100" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="品牌Id" prop="brandId">
							<el-input v-model="state.ruleForm.brandId" placeholder="请输入品牌Id" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="车系Id" prop="seriesId">
							<el-input v-model="state.ruleForm.seriesId" placeholder="请输入车系Id" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="车型Id" prop="modelId">
							<el-input v-model="state.ruleForm.modelId" placeholder="请输入车型Id" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="价格" prop="price">
							<el-input-number v-model="state.ruleForm.price" placeholder="请输入价格" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="新车指导价" prop="originalPrice">
							<el-input-number v-model="state.ruleForm.originalPrice" placeholder="请输入新车指导价" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="里程数" prop="mileage">
							<el-input-number v-model="state.ruleForm.mileage" placeholder="请输入里程数" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="车辆类型" prop="vehicleType">
							<el-input v-model="state.ruleForm.vehicleType" placeholder="请输入车辆类型" maxlength="20" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="变速箱类型" prop="transmission">
							<el-input v-model="state.ruleForm.transmission" placeholder="请输入变速箱类型" maxlength="20" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="燃油类型" prop="fuelType">
							<el-input v-model="state.ruleForm.fuelType" placeholder="请输入燃油类型" maxlength="20" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="排放标准" prop="emissionStandard">
							<el-input v-model="state.ruleForm.emissionStandard" placeholder="请输入排放标准" maxlength="20" show-word-limit clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="车况等级" prop="conditionLevel">
							<el-input-number v-model="state.ruleForm.conditionLevel" placeholder="请输入车况等级" clearable />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20" >
						<el-form-item label="状态" prop="status">
							<el-input-number v-model="state.ruleForm.status" placeholder="请输入状态" clearable />
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="() => state.showDialog = false">取 消</el-button>
					<el-button @click="submit" type="primary" v-reclick="1000">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select), :deep(.el-input-number) {
  width: 100%;
}
</style>